package com.qvtu.springboot.controller;

import com.github.pagehelper.PageInfo;
import com.qvtu.springboot.Common.Result;
import com.qvtu.springboot.entity.Employee;
import com.qvtu.springboot.service.EmployeeService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

//    增加数据
    @PostMapping("/add")
    public Result add(@RequestBody Employee employee){
        employeeService.add(employee);
        return Result.success("新增数据成功");
    }

//    删除数据
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id){
        employeeService.deleteById(id);
        return Result.success("删除成功");
    }

//    修改数据
    @PutMapping("/update")
    public Result update(@RequestBody Employee employee){
        employeeService.update(employee);
        return Result.success("成功修改数据");
    }

    //    查询全部数据
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Employee> list = employeeService.selectAll();
        return Result.success(list);
    }

    //  查询单个数据
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Employee employee = employeeService.selectById(id);
        return Result.success(employee);
    }

    //    @PathVariable 注解确实不能像 @RequestParam 那样直接设置默认值。
//    分页查询
//    pageNum 当前页码 默认值1
//    pageSize 每页的个数 默认值10
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Employee> pageInfo = employeeService.selectPage(pageNum, pageSize);
        return Result.success(pageInfo);
    }
}
